<?php

namespace App\Http\Controllers;

use App\Http\Resources\ContractResource;
use App\Laravue\Models\contract;
use App\Laravue\Models\examineFlow;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ContractController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $serach=$request->input('search');
        $media=$request->input('media');
        $list = contract::select('contracts.*','users.name','p.name as product_line_name','c.advertiser as adname','m.title as mht_title')
            ->leftJoin('customers as c','c.id','=','contracts.advertiser')
            ->leftJoin('users','users.id','=','contracts.submituser')
            ->leftJoin('product_lines as p','contracts.product_line','=','p.id')
            ->leftJoin('contracts as m','contracts.mht_id','=','m.id')
            ->when($serach,function($query) use ($serach){
                return $query
                    ->where('c.advertiser','like',"%$serach%")
                    ->orwhere('users.name','like',"%$serach%")
                    ->orwhere('p.name','like',"%$serach%")
                    ;
            })->when($media,function($q) use($media){
                return $q->where('contracts.type',$media);
            })
            ->orderBy('contracts.id','desc')
            ->paginate(15);

        return ContractResource::collection($list);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator =  $request->validate(
            [
                'advertiser' => ['required']
            ]
        );

        $params = $request->all();
        //如果是媒介合同
        if($params['type']==1)
        {
            if(empty($params['mht_id'])){
                throw new \Exception('没有找到媒体政策...');
            }
            $mht=contract::find($params['mht_id']);
            $params['product_line']=$mht->product_line;
            $flow_id=examineFlow::getFlowIdByType('contract');
        }elseif($params['type']==2)
        {
            $flow_id=examineFlow::getFlowIdByType('media_contract');
        }

        $model = new contract($params);
        $model->submituser = Auth::id();
        $model->flow_id=$flow_id;
        $model->save();
        return new ContractResource($model);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $one = contract::select('contracts.*','users.name','p.name as product_line_name','c.advertiser as adname','m.title as mht_title')
            ->leftJoin('customers as c','c.id','=','contracts.advertiser')
            ->leftJoin('users','users.id','=','contracts.submituser')
            ->leftJoin('product_lines as p','contracts.product_line','=','p.id')
            ->leftJoin('contracts as m','contracts.mht_id','=','m.id')
            ->where('contracts.id',$id)
            ->first();

        return new ContractResource($one);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $model=contract::where('id',$id)->update($request->all());
        return new ContractResource(['msg'=>'修改成功']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    //获取媒介合同列表
    public function getMcontract(){
        $list = contract::where([['type','2'],['status','1'],['check_status','2']])->orderBy('id','desc')->get();
        return ContractResource::collection($list);
    }
}
